#include<iostream>
#include<vector>
#include<stack>
#include<string>
#include<queue>
#include<algorithm>
#include<unordered_set>
using namespace std;



class Solution{
    private:
    

    public:
    //bfs
    vector<double> ans1(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries){
        int nvar = 0;
        unordered_map<string, int> P;

        int len = equations.size();
        for(int i = 0; i != len; i++){
            if(P.find(equations[i][0]) == P.end()){
                P[equations[i][0]] = nvar;
                nvar++;
            }
            if(P.find(equations[i][1]) == P.end()){
                P[equations[i][1]] = nvar;
                nvar++;
            }
        }

        vector<vector<pair<int, double>>> edge(nvar);
        for(int i = 0; i != len; i++){
            int va = P[equations[i][0]], vb = P[equations[i][1]];
            edge[va].push_back({vb, values[i]});
            edge[vb].push_back({va, 1/values[i]});
        }

        vector<double> ret;
        for(auto& q: queries){
            double result = -1.0;
            if(P.find(q[0]) != P.end() && P.find(q[1]) != P.end()){
                int ia = P[q[0]], ib = P[q[1]];
                if(ia == ib){
                    result = 1.0;
                }else{
                    queue<int> points;
                    points.push(ia);
                    vector<double> ratios(nvar, -1.0);
                    ratios[ia] = 1.0;

                    while(!points.empty() && ratios[ib] < 0){
                        int x = points.front();
                        points.pop();

                        for(auto& [y, val]: edge[x]){
                            if(ratios[y] < 0){
                                ratios[y] = ratios[x] * val;
                                points.push(y);
                            }
                        }
                    }
                    result = ratios[ib];
                }
            }
            ret.push_back(result);
        }

        return ret;
    }

};

void show(vector<auto>& val){
    int len = val.size();
    for(int i = 0; i != len-1; i++){
        cout << val[i] << " " << ends;
    }
    cout << val[len-1] << endl;
}



void test1(){
    vector<vector<string>> equations = {
        {"a","b"},
        {"b","c"}
    };
    vector<double> values = {2.0,3.0};
    vector<vector<string>> queries = {
        {"a","c"},
        {"b","a"},
        {"a","e"},
        {"a","a"},
        {"x","x"}
    };

    Solution s;
    vector<double> ret = s.ans1(equations, values, queries);
    show(ret);
    
}



int main(void){
    test1();

    return 0;
}